# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=glib2
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.41.1
pkgrel=1
url="http://www.gtk.org/"
arch=('any')
pkgdesc="Common C routines used by GTK+ 2.4 and other libs (mingw-w64)"
license=('LGPL')
options=('!debug' 'strip' 'staticlibs')
install=glib2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
        "${MINGW_PACKAGE_PREFIX}-gettext"
        "${MINGW_PACKAGE_PREFIX}-zlib"
        "${MINGW_PACKAGE_PREFIX}-libffi")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "python2")
source=("http://ftp.gnome.org/pub/GNOME/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz"
        0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
        0003-g_abort.all.patch
        0004-glib-prefer-constructors-over-DllMain.patch
        0005-glib-send-log-messages-to-correct-stdout-and-stderr.patch
        0015-fix-stat.all.patch
        0016-fix-separators-in-compiled-resources.mingw.patch
        0017-glib-use-gnu-print-scanf.patch
        0018-fix-tests-to-compile.all.patch
        0020-fix-warnings.all.patch
        0021-use-64bit-stat-for-localfile-size-calc.all.patch
        0022-make-sure-errno-survives-gettext.all.patch
        0023-print-in-binary-more-for-testing.all.patch
        0024-support-pthread-cond-timewait-relative.all.patch
        0025-glib-update-valgrind-h-stdint.patch)
md5sums=('bae557c87323e8df7ac30fb484b5cba9'
         '98c7778b5e8a30dbcdb86bcd15f9c11d'
         '6eb9d56028ea3bbec3d053254a00a04b'
         '46969e815f3ab19582ba33667a52048b'
         '7b065f9bc154bf996523974a698b24de'
         '963a72f6efcaa3405204b76b27b44645'
         'c28b7514169e85262fda71a3da53529e'
         '29065eb21bc779787080f0529a6c5f96'
         '592a01b9020f24e857f7681bfc6ae431'
         'b38fa2a321731a705bbea5a67a0b9dac'
         '952989e534235721d30b9ee3d89cc225'
         'dcefd933fca3a11080db4ec05491ba40'
         '217a997ede1b93ac06f96ed869b74378'
         '30e62fefcec4eef9e1a7f60eba43a97c'
         'c5f7671c710678473b3e6209d57710b1')

prepare() {
  cd "$srcdir/glib-$pkgver"

  patch -Np1 -i "$srcdir/0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch"
  patch -Np1 -i "$srcdir/0003-g_abort.all.patch"
  patch -Np1 -i "$srcdir/0004-glib-prefer-constructors-over-DllMain.patch"
  patch -Np0 -i "$srcdir/0005-glib-send-log-messages-to-correct-stdout-and-stderr.patch"
  patch -Np1 -i "$srcdir/0015-fix-stat.all.patch"
  patch -Np1 -i "$srcdir/0016-fix-separators-in-compiled-resources.mingw.patch"
  patch -Np1 -i "$srcdir/0017-glib-use-gnu-print-scanf.patch"
  patch -Np1 -i "$srcdir/0018-fix-tests-to-compile.all.patch"
  patch -Np1 -i "$srcdir/0020-fix-warnings.all.patch"
  patch -Np1 -i "$srcdir/0021-use-64bit-stat-for-localfile-size-calc.all.patch"
  patch -Np1 -i "$srcdir/0022-make-sure-errno-survives-gettext.all.patch"
  patch -Np1 -i "$srcdir/0023-print-in-binary-more-for-testing.all.patch"
  #patch -Np1 -i "$srcdir/0024-support-pthread-cond-timewait-relative.all.patch"
  patch -Np1 -i "$srcdir/0025-glib-update-valgrind-h-stdint.patch"

  NOCONFIGURE=1 ./autogen.sh
}

build()
{
  cd "$srcdir/glib-$pkgver"

  mkdir $srcdir/build-${CARCH}-static && cd $srcdir/build-${CARCH}-static
  ../glib-$pkgver/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-shared --enable-static \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make

  mkdir $srcdir/build-${CARCH}-shared && cd $srcdir/build-${CARCH}-shared
  ../glib-$pkgver/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-static \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make
}

package() {
  cd "$srcdir/build-${CARCH}-shared"
  make DESTDIR="$pkgdir" install

  cd "$srcdir/build-${CARCH}-static"
  make DESTDIR="$pkgdir/static" install

  cd "$srcdir/build-${CARCH}-shared"
  mv "$pkgdir/static/${MINGW_PREFIX}/lib/"*.a "$pkgdir/${MINGW_PREFIX}/lib/"

  rm -rf "${pkgdir}/static"

  rm -f "${pkgdir}${MINGW_PREFIX}"/lib/charset.alias
  #rm -rf "${pkgdir}${MINGW_PREFIX}"/{share/{gtk-doc,man,bash-completion,gdb},lib/charset.alias,etc}
  rm "${pkgdir}${MINGW_PREFIX}/lib/"*.def

  #rm -f "${pkgdir}${MINGW_PREFIX}/bin/gdbus-codegen"
  #rm -rf "${pkgdir}${MINGW_PREFIX}/lib/gdbus-2.0"
}
